package com.it.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.travel.dao.CategoryMapper;
import com.it.travel.dao.CategoryProductMapper;
import com.it.travel.dao.ProductMapper;
import com.it.travel.pojo.Category;
import com.it.travel.pojo.CategoryProduct;
import com.it.travel.service.HotelService;
import com.it.travel.vo.HotelVO;
import com.it.travel.vo.Result;
import com.it.travel.vo.RouteVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class HotelServiceImpl implements HotelService {
    @Resource
    private ProductMapper productMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CategoryProductMapper categoryProductMapper;

    @Override
    public Result<?> getHotelInfo(Integer num) {

        Category category2 = categoryMapper.selectOne(new QueryWrapper<Category>().eq("name", "酒店"));
        List<HotelVO> set = new ArrayList<>();
        List<HotelVO> list = productMapper.queryHotelInfo(category2.getId(), num);
        for (HotelVO hotelVO : list) { // 查询出的数据
            QueryWrapper<CategoryProduct> wrapper = new QueryWrapper<>();
            wrapper.eq("category_id", hotelVO.getCategoryId());
            wrapper.eq("product_id", hotelVO.getId());
            List<CategoryProduct> categoryProducts = categoryProductMapper.selectList(wrapper);// 如果查询出来的数据的分类在中间表可以查询到
            if (categoryProducts.size() > 0) {
                set.add(hotelVO);
            }
        }

        Iterator<HotelVO> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println("iterator.next() = " + iterator.next());
        }
        return Result.success(set, "yes");
    }
}
